package com.xsm.DataBase;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.xsm.activitygroup.BookMessage;
import com.xsm.internet.PostMessage;
import com.xsm.object.BookObject;
import com.xsm.object.BookTypeObject;
import com.xsm.object.ClassifiObject;
import com.xsm.object.QuanziObject;

import android.R.integer;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class SaveMessage {
	DataHelper helper;
	Context context;
	String userid;
	SQLiteDatabase sqLiteDatabase;
	public SaveMessage(Context context,String userid){
		this.context=context;
		this.userid=userid;
		helper=new DataHelper(context,"XSM",null,1);
		sqLiteDatabase=helper.getWritableDatabase();
	}
	
	public SaveMessage(Context context){
		this.context=context;
		helper=new DataHelper(context,"XSM",null,1);
		sqLiteDatabase=helper.getWritableDatabase();	
	}
	
	
public void saveUserMessage(ArrayList<String> msg){
String sql="insert into '"+userid+"Message' values(?,?,?,?,?,?,?,?,?,?)";
try {
	String ms[]=new String[msg.size()];
	for(int i=0;i<msg.size();i++){
		ms[i]=msg.get(i);
	}
sqLiteDatabase.delete("'"+userid+"Message'", null, null);		
sqLiteDatabase.execSQL(sql,ms);
} catch (Exception e) {
	Log.d("my",e+"");
}
if(sqLiteDatabase.isOpen())
sqLiteDatabase.close();
helper.close();
}


		

public void UpdateMessage(String userid,String type,String value){
	String sql="update '"+userid+"Message' set '"+type+"'='"+value+"' where userid='"+userid+"'";
	try {
	sqLiteDatabase.execSQL(sql);	
	sqLiteDatabase.close();
	helper.close();
	} catch (Exception e) {
		Log.d("my", e+"");
	}
}




public void initType(){
	boolean type1=false,type2=false,type3=false;
	sqLiteDatabase.delete("textbooka",null,null);
	sqLiteDatabase.delete("textbookb",null,null);
	sqLiteDatabase.delete("textbookc",null,null);
String sqla="select * from textbooka";
Cursor cursora=sqLiteDatabase.rawQuery(sqla,null);
	while(cursora.moveToNext()){
		type1=true;
	}

	String sqlb="select * from textbookb";
	Cursor cursorb=sqLiteDatabase.rawQuery(sqlb,null);
		while(cursorb.moveToNext()){
			type2=true;
    }
	
	String sqlc="select * from textbookc";
	Cursor cursorc=sqLiteDatabase.rawQuery(sqlc,null);
	while(cursorc.moveToNext()){
	type3=true;
    }	
 if(type1==false){
		List params=new ArrayList();
		params.add(new BasicNameValuePair("ASK","getTypeA"));
		PostMessage postMessage=new PostMessage(params);
		String result=postMessage.getResult();		
		 String s[] = null;
		try {
			JSONArray array=new JSONArray(result);
			for(int i=0;i<array.length();i++){
		      s=new String[4];	
		     JSONObject object=array.optJSONObject(i);
		     s[0]=object.getString("id");
		     s[1]=object.getString("name");
		     s[2]="";
		     s[3]="";
		 	String insert="insert into textbooka values(?,?,?,?)";		
			sqLiteDatabase.execSQL(insert,s);
			}
		} catch (JSONException e) {	
	Log.d("my", e+"");
	type1=false;
		}
if(!result.equals("-1"))		
type1=true;		
  }
	
 if(type2==false){
		List params=new ArrayList();
		params.add(new BasicNameValuePair("ASK","getTypeB"));
		PostMessage postMessage=new PostMessage(params);
		String result=postMessage.getResult();	
		 String s[] = null;
		try {
			JSONArray array=new JSONArray(result);
			for(int i=0;i<array.length();i++){
		      s=new String[4];	
		     JSONObject object=array.optJSONObject(i);
		     s[0]=object.getString("id");
		     s[1]=object.getString("name");
		     s[2]=object.getString("parentid");
		     s[3]="";
		 	String insert="insert into textbookb values(?,?,?,?)";		
			sqLiteDatabase.execSQL(insert,s);	
			}
	
		} catch (JSONException e) {	
			e.printStackTrace();
			type2=false;	
		}
	if(!result.equals("-1"))	
	type2=true;	
}
 if(type3==false){
		List params=new ArrayList();
		params.add(new BasicNameValuePair("ASK","getTypeC"));
		PostMessage postMessage=new PostMessage(params);
		String result=postMessage.getResult();	
		 String s[] = null;
		try {
			JSONArray array=new JSONArray(result);
			for(int i=0;i<array.length();i++){
		      s=new String[4];	
		     JSONObject object=array.optJSONObject(i);
		     s[0]=object.getString("id");
		     s[1]=object.getString("name");
		     s[2]=object.getString("parentid"); 
		     s[3]=object.getString("gparentid");
		 	String insert="insert into textbookc values(?,?,?,?)";
			sqLiteDatabase.execSQL(insert,s);
			}
		} catch (JSONException e) {	
			e.printStackTrace();
			type3=false;
		}
		if(!result.equals("-1"))	
		type3=true;
}
 SharedPreferences sharedPreferences=context.getSharedPreferences("XSM", 0);
 if(type1==true&&type2==true&&type3==true){
   sharedPreferences.edit().putString("Type", "hasdata").commit();
 }
 
 sqLiteDatabase.close();
 cursora.close();
 cursorb.close();
 cursorc.close();
 helper.close();
}


  public String initRecommend(){
		List params=new ArrayList();
		params.add(new BasicNameValuePair("ASK","GetRecommend"));
		PostMessage postMessage=new PostMessage(params);
		String result=postMessage.getResult();		
		try {
			JSONArray array=new JSONArray(result);
			if(array.length()>0){
		   sqLiteDatabase.delete("Recommend", null, null);	
			for(int i=0;i<array.length();i++){
				JSONObject object=array.optJSONObject(i);
				String sql="insert into Recommend (rid,gparentname,classify)values('"+object.getString("rid")+"','"+object.getString("gparentname")+"','"+object.getString("classify")+"')";
			   sqLiteDatabase.execSQL(sql);
			}}else {
         return "-1";	
			}
		} catch (JSONException e) {
	       Log.d("my", e+"");
	       return "-1";
   	}
		if(sqLiteDatabase.isOpen())
			sqLiteDatabase.close();
			helper.close();	
			return "1";
}


public ArrayList<String> getTypeA(){
	ArrayList<String> arrayList=new ArrayList<String>();
String sqlString="select * from textbooka ";
Cursor cursor=sqLiteDatabase.rawQuery(sqlString,null);
while (cursor.moveToNext()) {
arrayList.add(cursor.getString(cursor.getColumnIndex("namea")));	
}
cursor.close();
sqLiteDatabase.close();
helper.close();
return arrayList;
}


public ArrayList<String> getTypeB(int parentid){
	ArrayList<String> arrayList=new ArrayList<String>();
	String sqlString="select * from textbookb where parentid='"+parentid+"'";
	Cursor cursor=sqLiteDatabase.rawQuery(sqlString,null);
	while (cursor.moveToNext()) {
		arrayList.add(cursor.getString(cursor.getColumnIndex("nameb")));	
	}
	helper.close();
	cursor.close();
	sqLiteDatabase.close();
	return arrayList;
}

public int getTypecSize(){
	int i=0;
	String sqlString="select * from textbookb ";
	Cursor cursor=sqLiteDatabase.rawQuery(sqlString,null);
	while (cursor.moveToNext()) {
		i=i+1;
	}
	cursor.close();
	sqLiteDatabase.close();
	helper.close();
	return i;
}

public ArrayList<ArrayList<ArrayList<ClassifiObject>>> getTypeC(){	
	int j=0;
	int i=0;
	ArrayList<ArrayList<ArrayList<ClassifiObject>>> typeArrayList=new ArrayList<ArrayList<ArrayList<ClassifiObject>>>();
	String sql1="select * from textbooka";
	Cursor cursor=sqLiteDatabase.rawQuery(sql1,null);
	while(cursor.moveToNext()){
		ArrayList<ArrayList<ClassifiObject>> arrayList1=new ArrayList<ArrayList<ClassifiObject>>();
		String sql2="select * from textbookb where parentid='"+j+"'";
		Cursor cursor1=sqLiteDatabase.rawQuery(sql2,null);
		while(cursor1.moveToNext()){	
			ArrayList<ClassifiObject> arrayList=new ArrayList<ClassifiObject>();
			String sqlString="select * from textbookc where parentid='"+i+"'";
			Cursor cursor2=sqLiteDatabase.rawQuery(sqlString,null);
			while (cursor2.moveToNext()) {
				ClassifiObject object=new ClassifiObject();
				object.name=cursor2.getString(cursor2.getColumnIndex("namec"));
				object.parentid=cursor2.getString(cursor2.getColumnIndex("textbookcid"));
				arrayList.add(object);	
			}
		arrayList1.add(arrayList);	
		i=i+1;
		}
		j=j+1;
		typeArrayList.add(arrayList1);
	}
	
     cursor.close();
	sqLiteDatabase.close();
	helper.close();
	return typeArrayList;
}

public void initPress(){	
	List params=new ArrayList();
	params.add(new BasicNameValuePair("ASK","getPress"));
	PostMessage postMessage=new PostMessage(params);
	String result=postMessage.getResult();	
    try {
    	String value[]=new String[2];
		JSONArray array=new JSONArray(result);
		for(int i=0;i<array.length();i++){
		JSONObject object=array.optJSONObject(i);		
		value[0]=object.getString("id");
		value[1]=object.getString("name");
		String sql="insert into press values(?,?)";
		sqLiteDatabase.execSQL(sql, value);
		SharedPreferences sharedPreferences=context.getSharedPreferences("XSM", 0);
		sharedPreferences.edit().putString("Press", "hasdata").commit();
		}
	} catch (Exception e) {
		e.printStackTrace();
	}	
	sqLiteDatabase.close();
	helper.close();
}

public ArrayList<String> getPress(){
	ArrayList<String> press=new ArrayList<String>();
	String sql="select * from press";
	Cursor cursor=sqLiteDatabase.rawQuery(sql,null);
	while(cursor.moveToNext()){
		String name=cursor.getString(cursor.getColumnIndex("pressname"));
     	press.add(name);
	}
	  cursor.close();
	  sqLiteDatabase.close();
	  helper.close();
	return press;
}
  
public void SaveBooks(ArrayList<BookObject> bookarray){
	if(bookarray.size()>0){
		try {
			sqLiteDatabase.delete("Books", null, null);
		} catch (Exception e) {
		}	
	}
		
	for(int i=0;i<bookarray.size();i++){
		BookObject books=bookarray.get(i);
		String s[]=new String[13];
		s[0]=books.getGoodsid();
		s[1]=books.getBookName();
		s[2]=books.getPress();
		s[3]=books.getPlace();
		s[4]=books.getTime();
		s[5]=books.getUserid();
		s[6]=books.getOner();
		s[7]=books.getMessage();
		s[8]=books.getReleaseType();
		s[9]=books.getBookType();
		s[10]=books.getPrice();
		s[11]=books.getPhone();
		s[12]=books.getGparentid();
		String sql="insert into Books values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
		
		try {
			sqLiteDatabase.execSQL(sql, s);
		} catch (Exception e) {
			Log.d("my", e+"");
		}
	}
	sqLiteDatabase.close();
	helper.close();
}

public ArrayList<BookObject> Getbookarray(String place ,String booktype,String selltype){
	String sql;
	ArrayList<BookObject> arrayList=new ArrayList<BookObject>();
	if(booktype.equals("全部"))
	 sql="select * from Books where school='"+place+"' and buytype='"+selltype+"'";
	else
	   sql="select * from Books where school='"+place+"' and booktype='"+booktype+"' and buytype='"+selltype+"'";
	Cursor cursor=sqLiteDatabase.rawQuery(sql, null);
	while (cursor.moveToNext()) {
	BookObject object=new BookObject();
	object.setBookName(cursor.getString(cursor.getColumnIndex("bookname")));
	object.setGoodsid(cursor.getString(cursor.getColumnIndex("goodsid")));
	object.setPress(cursor.getString(cursor.getColumnIndex("press")));
	object.setPlace(cursor.getString(cursor.getColumnIndex("school")));
	object.setTime(cursor.getString(cursor.getColumnIndex("time")));
	object.setOner(cursor.getString(cursor.getColumnIndex("username")));
	object.setMessage(cursor.getString(cursor.getColumnIndex("explain")));
	object.setReleaseType(cursor.getString(cursor.getColumnIndex("buytype")));
	object.setBookType(cursor.getString(cursor.getColumnIndex("booktype")));
	object.setPrice(cursor.getString(cursor.getColumnIndex("price")));
	object.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
	object.setUserid(cursor.getString(cursor.getColumnIndex("userid")));
	object.setGparentid(cursor.getString(cursor.getColumnIndex("gparentid")));
	arrayList.add(object);
	}
	cursor.close();
	sqLiteDatabase.close();
	return arrayList;
}

public ArrayList<String> getTypec(int parentid){
	ArrayList<String> arrayList=new ArrayList<String>();
	String sql="select * from textbookc where parentid='"+parentid+"'";
	Cursor cursor=sqLiteDatabase.rawQuery(sql, null);
	while (cursor.moveToNext()) {
   arrayList.add(cursor.getString(cursor.getColumnIndex("namec")));
	}
	cursor.close();
	sqLiteDatabase.close();
	return arrayList;
}

public void SaveQuanzi(ArrayList<QuanziObject> values){

		try {
			sqLiteDatabase.delete("Quanzi", null, null);
		} catch (Exception e) {
			
		}	

	
	for(int i=0;i<values.size();i++){
		QuanziObject object=values.get(i);
		String s[]=new String[7];
		s[0]=object.quanziid;
		s[1]=object.content;
		s[2]=object.time;
		s[3]=object.user;
		s[4]=object.username;
		s[5]=object.zanCount;
		s[6]=object.ruoCount;
		String insert="insert into Quanzi values(?,?,?,?,?,?,?)";
	try {
		sqLiteDatabase.execSQL(insert, s);
	  } catch (Exception e) {
		  Log.d("my", e+"");
	 }
	}
	sqLiteDatabase.close();
}

public ArrayList<QuanziObject> getQuanzi(){
	ArrayList<QuanziObject> arrayList=new ArrayList<QuanziObject>();
	String sqlString="select * from Quanzi";
	try {
		Cursor cursor=sqLiteDatabase.rawQuery(sqlString,null);
		while (cursor.moveToNext()) {
			QuanziObject object=new QuanziObject();
			object.quanziid=cursor.getString(cursor.getColumnIndex("quanziid"));
			object.user=cursor.getString(cursor.getColumnIndex("userid"));
			object.username=cursor.getString(cursor.getColumnIndex("username"));
			object.content=cursor.getString(cursor.getColumnIndex("content"));
			object.time=cursor.getString(cursor.getColumnIndex("time"));
			object.zanCount=cursor.getString(cursor.getColumnIndex("zancount"));
			object.ruoCount=cursor.getString(cursor.getColumnIndex("ruocount"));
			arrayList.add(object);
			}
		cursor.close();
		sqLiteDatabase.close();
	} catch (Exception e) {
		
	}	
	return arrayList;
}

public void saveNews(String content){
	String in="insert into News (id,content)values('1','"+content+"')";
	sqLiteDatabase.delete("News", null, null);
	try {
		sqLiteDatabase.execSQL(in);
	} catch (Exception e) {
	Log.d("my", e+"");
	}
   sqLiteDatabase.close();
}

public String getnews(){
String sql="select * from News ";
String reString="";
	try {
		Cursor cursor=sqLiteDatabase.rawQuery(sql,null);
		if (cursor.moveToNext()) {
	reString=cursor.getString(cursor.getColumnIndex("content"));
		}	
		cursor.close();
		sqLiteDatabase.close();
return	reString;
	} catch (Exception e) {
	}
	return "";
}


public ArrayList<String> getrecommend(){
	ArrayList<String> arrayList=new ArrayList<String>();
		String sql="select * from Recommend";
		Cursor cursor=sqLiteDatabase.rawQuery(sql,null);
		while (cursor.moveToNext()) {
			String title=cursor.getString(cursor.getColumnIndex("gparentname"));
			String string=cursor.getString(cursor.getColumnIndex("classify"));
		
			arrayList.add(string);
		}
		cursor.close();
		sqLiteDatabase.close();
	return arrayList;
}
}
